package one.bugu.android.demon.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.DecimalFormat;

import one.bugu.android.demon.R;

public class DialogMedal extends Dialog {

    private Context mContext;
    private ImageView iv_stri_cancle;
    private TextView tv_stri_btn;
    private RelativeLayout rl_container;
    private ImageView iv_sna;
    private TextView tv_level_title;
    private LinearLayout ll_cal_container;
    private ImageView iv_cal_medal;


    public DialogMedal(@NonNull Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public DialogMedal(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        initView();
    }

    protected DialogMedal(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        initView();
    }

    private void initView() {
        View view = View.inflate(mContext, R.layout.dialog_medal, null);
        setContentView(view);
        iv_stri_cancle = view.findViewById(R.id.iv_stri_cancle);
        rl_container = view.findViewById(R.id.rl_container);
        iv_sna = view.findViewById(R.id.iv_sna);
        tv_level_title = view.findViewById(R.id.tv_level_title);
        tv_stri_btn = view.findViewById(R.id.tv_stri_btn);
        ll_cal_container = view.findViewById(R.id.ll_cal_container);
        iv_cal_medal = view.findViewById(R.id.iv_cal_medal);

        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        WindowManager wm = this.getWindow().getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        lp.width = width - 100;
        dialogWindow.setAttributes(lp);
        this.setCanceledOnTouchOutside(false);

        tv_stri_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onShareBtnClick != null) {
                    onShareBtnClick.onGetBtnClickListener();
                    DialogMedal.this.dismiss();
                }
            }
        });
        iv_stri_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMedal.this.dismiss();
            }
        });
    }


    private OnGetBtnClick onShareBtnClick;

    public interface OnGetBtnClick {
        void onGetBtnClickListener();
    }

    public void setOnGetBtnClick(OnGetBtnClick onShareBtnClick) {
        this.onShareBtnClick = onShareBtnClick;
    }

    /**
     * 设置背景
     *
     * @param bgRes
     */
    public void setDialogBg(int bgRes) {
        if (rl_container != null) {
            rl_container.setBackgroundResource(bgRes);
        }
    }

    /**
     * 设置头衔
     *
     * @param imageRes
     */
    public void setImageRes(int imageRes) {
        if (iv_sna != null) {
            iv_sna.setImageResource(imageRes);
            iv_cal_medal.setImageResource(imageRes);
        }
    }

    /**
     * 设置头衔名称
     *
     * @param title
     */
    public void setLevelTitle(String title) {
        if (tv_level_title != null) {
            tv_level_title.setText(title);
        }
    }

    public void setStatus(boolean isBgt) {
        ll_cal_container.setVisibility(isBgt ? View.GONE : View.VISIBLE);
        iv_sna.setVisibility(isBgt ? View.VISIBLE : View.GONE);
    }

}
